package com.artifact.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.artifact.dao.UserGeneralizeDao;
import com.artifact.model.UserGeneralize;

@Repository("userGeneralizeDao")
public class UserGeneralizeDaoImpl implements UserGeneralizeDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean addUserGeneralize(String userid, String generalizeId) {
		String sql = "INSERT INTO `yungou_assist`.`usergeneralize` (`userid`, `generalizeId`) VALUES (?, ?)";
		return jdbcTemplate.update(sql, userid, generalizeId) > 0;
	}

	@Override
	public boolean updateIsGetRewards(String userid) {
		String sql = "UPDATE `yungou_assist`.`usergeneralize` SET `isGetRewards`=1, `getRewardsTime`=now() WHERE `userid`=?";
		return jdbcTemplate.update(sql, userid) > 0;
	}

	@Override
	public UserGeneralize getUserGeneralize(String userid) {
		String sql = "select * from usergeneralize where `userid` = ?";
		try {
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserGeneralize>(UserGeneralize.class), userid);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<UserGeneralize> getUserGeneralizes(String generalizeId) {
		String sql = "select * from usergeneralize where `generalizeId` = ?";
		try {
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserGeneralize>(UserGeneralize.class), generalizeId);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public int getUserGeneralizeListNum() {
		String sql = "select count(id) as id from usergeneralize";
		return jdbcTemplate.queryForObject(sql, Integer.class);
	}

	@Override
	public List<UserGeneralize> getUserGeneralizeList(int firstPage, int pageSize, int sortType, boolean rankType) {
		String sql = "select * from usergeneralize limit ?,?";
		try {
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserGeneralize>(UserGeneralize.class), firstPage * pageSize,
					pageSize);
		} catch (Exception e) {
			return null;
		}
	}

}
